#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "util.h"

class MainWindow2 : public QWidget
{
    Q_OBJECT

public:
    QLineEdit* l1;
    QLineEdit* l2;
    QLineEdit* l3;
    QLineEdit* l4;

    QLineEdit* l_gap;
    QLineEdit* l_bigger;
    QLineEdit* l_co;

    explicit MainWindow2(QWidget *parent = nullptr)
    {
        this->setAttribute( Qt::WA_DeleteOnClose );
        QHBoxLayout *h = new QHBoxLayout();


        QVBoxLayout *v = new QVBoxLayout();
        v->addLayout(h);

        l1 = new QLineEdit(QString::number( xs));
        l2 = new QLineEdit(QString::number( ys));
        l3 = new QLineEdit(QString::number( xl));
        l4 = new QLineEdit(QString::number( yl));
        h->addWidget(l1);
        h->addWidget(l2);
        h->addWidget(l3);
        h->addWidget(l4);

        l_gap = new QLineEdit(QString::number( pic_gap));
        v->addWidget(l_gap);
        l_bigger = new QLineEdit(QString::number( bigger));
        v->addWidget(l_bigger);

        auto qb = new QPushButton("Se&t");
        v->addWidget(qb);
        connect(qb, SIGNAL(clicked()), this, SLOT(onCli()));
        l_co = new QLineEdit(QString::number( 0));
        v->addWidget(l_co);
        this->setLayout(v);
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(onT()));
        timer->start(1000);

    }
public slots:
    void onCli()
    {
        xs =  l1->text().toInt();
        ys =  l2->text().toInt();
        xl =  l3->text().toInt();
        yl =  l4->text().toInt();
        pic_gap =  l_gap->text().toInt();
        bigger =  l_bigger->text().toInt();

        if(xl*bigger>1366)
        {
            xl = 1366/bigger;
        }
        if(yl*bigger>768)
        {
            yl=768/bigger;
        }
    }

    void onT()
    {
        l_co->setText(QString::number( drawco));
    }
};


class MainWindow : public QWidget
{
    Q_OBJECT
public:
    qthr * q;
int co=0;
    explicit MainWindow(QWidget *parent = nullptr)
    {
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(onT()));
        timer->start(20);
        q = new qthr();
        q->start();
    }


void  paintEvent(QPaintEvent* e)
{
    QWidget::paintEvent(e);
    QPainter painter(this);
    qDebug()<<"draw "<<co;
    painter.drawImage(QRect(0,0,g_img.width(),g_img.height()),g_img);
}


public slots:

    void onT()
    {
        if (co!=drawco)
        {
            co=drawco;
            update();
        }
    }

};

#endif
